package housekeeping.service.Impl;

import housekeeping.mapper.ServiceMapper;
import housekeeping.pojo.Serve;
import housekeeping.service.ServeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import housekeeping.util.ServerResult;

import java.util.List;

/**
 * @version 1.0
 * @Author Du
 * @Date 2025/7/7 17:42
 * @注释
 */
@Service
public class ServeServiceImpl implements ServeService {
    @Autowired
    private ServiceMapper serviceMapper;
    @Override
    public List<Serve> getServices() {
        List<Serve> list = serviceMapper.selectList(null);
        return list;
    }
    @Override
    public ServerResult<?> insertService(Serve service){
        int result = serviceMapper.insertPartial(service.getCategory(),service.getDiscription(),service.getStandardPrice(),service.getStandardDuration());
        if(result < 1) {
            return ServerResult.error(401, "添加失败");
        }else{
            return ServerResult.ok("添加成功");
        }
    }

    @Override
    public void deleteService(Integer id){
        int result = serviceMapper.deleteById(id);
        if(result < 1){
            throw  new RuntimeException("删除失败");
        }
    }
    @Override
    public Serve selectById(Integer id){
        Serve serve = serviceMapper.selectById(id);
        if(serve != null){
            return serve;
        }
        else{
            throw new RuntimeException("查询失败");
        }
    }
    @Override
    public ServerResult<?> updateService(Serve service){
        Serve oldServe = serviceMapper.selectById(service.getId());
        if(oldServe != null){
            int result = serviceMapper.updatePartial(service.getId(),service.getCategory(),service.getDiscription(),service.getStandardPrice(),service.getStandardDuration());
            if(result < 1){
                return ServerResult.ok("更新成功");
            }
        }
        else{
            return ServerResult.error(400,"更新失败");
        }
        return ServerResult.error(300,"空对象");
    }
}
